package com.dtsz.m2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.android.volley.Request;
import com.dtsz.m2.R;
import com.dtsz.m2.config.UrlConfig;
import com.dtsz.m2.ui.base.BaseActivity;
import com.dtsz.m2.ui.model.RegisterVo;
import com.dtsz.m2.ui.model.Users;
import com.dtsz.m2.utils.RSAEncrypt;
import com.dtsz.m2.utils.StringUtils;
import com.dtsz.m2.volley.state.HUCState;

public class LoginAuthorActivity extends BaseActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_login_author);
        super.onCreate(savedInstanceState);
    }

    private EditText etName;
    private EditText etPassword;
    private Button buPwd;
    private RegisterVo registerVo = new RegisterVo();


    @Override
    public void initView() {
        super.initView();
        etName = findViewById(R.id.et_name);
        etPassword = findViewById(R.id.et_password);
        buPwd = findViewById(R.id.bt_pwd);

    }

    @Override
    public void initClick() {
        super.initClick();
        buPwd.setOnClickListener(this);
        findViewById(R.id.iv_break).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.bt_pwd:
                String phoneStr = etName.getText().toString();
                if (StringUtils.isEmpty(phoneStr)) {
                    Toast.makeText(this, "登录账号不能为空", Toast.LENGTH_LONG).show();
                    return;
                }
                String pwdStr = etPassword.getText().toString();
                if (StringUtils.isEmpty(pwdStr)) {
                    Toast.makeText(this, "密码不能为空", Toast.LENGTH_LONG).show();
                }
                loading();
                registerVo.setPhone(phoneStr);
                registerVo.setPassword(RSAEncrypt.encrypt(pwdStr));
                hucRequestInterfase.executeStr(this, Users.class,
                        HUCState.HUC_REQEUST_CODE_1, Request.Method.POST, UrlConfig.LOGIN_URL,
                        hucParameInterfase.getRequestParameter(registerVo), true);
                break;
            case R.id.iv_break:
                finish();
                break;
        }
    }

    @Override
    public void onSuccessfulStrType(int inputType, Object response) {
        super.onSuccessfulStrType(inputType, response);
        switch (inputType) {
            case HUCState.HUC_REQEUST_CODE_1:
                if (response instanceof Users) {
                    Users users = (Users) response;
                    setSharedPreferences("tokenRSA", users.getBuToken());
                    setSharedPreferences("phone", users.getBuPhone());
                    setSharedPreferences("name", users.getBuName());
                    setSharedPreferences("buCertification", users.getBuCertification());
                    finish();
                } else {
                    Toast.makeText(this, response.toString(), Toast.LENGTH_SHORT).show();
                }
                break;
        }
        loadDismiss();
    }
}
